package com.wcgksoft.controller.admin;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.MsgInfo;
import com.wcgksoft.entity.admin.Account;
import com.wcgksoft.entity.admin.Authority;
import com.wcgksoft.service.admin.AccountService;
import com.wcgksoft.service.admin.AuthorityService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.CriteriaBuilder;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 御风
 * @version V1.0
 * @Package com.wcgksoft.controller.admin
 * @Description TODO 代理商给销售员开设账号
 * @date 2020/5/17 7:41
 * @ClassName AgentAccountController
 */
@Controller
@RequestMapping("system/agent")
public class AgentAccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private AuthorityService authorityService;

    @ResponseBody
    @RequestMapping(value = "save")
    public Layui save(@RequestParam(value = "data",required = false)String data) throws ParseException {
        JSONObject param = JSONObject.fromObject(data);
        Account account=null;
        if (null==param){
            return Layui.data(1, "请求格式有误", 0, null);
        }else{
            String ids=(String)param.get("id");
            if (ids.equals("")){
                account = (Account) JSONObject.toBean(JSONObject.fromObject(data), Account.class);
                account.setStatus(1);
                account.setCreateDate(new Date());
                account.setUserNumber(0);
                account.setSaleNumber(0);
                BigDecimal bigDecimal = new BigDecimal("0");
                account.setSaleMoney(bigDecimal);
                account.setSaleNumber(0);
                Authority authority = authorityService.findById(4);
                account.setAuthority(authority);
                accountService.save(account);
            }else{
                account = (Account) JSONObject.toBean(JSONObject.fromObject(data), Account.class);
                account.setStatus(1);
                Authority authority = authorityService.findById(4);
                account.setAuthority(authority);
                accountService.saveMerge(account);
            }

            return Layui.data(0, "success", 1, null);
        }
    }


    @ResponseBody
    @RequestMapping(value = "findAllSale")
    public Layui findAll(@RequestParam(value = "page",required = false)Integer page,
                         @RequestParam(value = "limit",required = false)Integer limit,
                         @RequestParam(value = "keyWord",required = false)String keyWord){
        DetachedCriteria criteria = DetachedCriteria.forClass(Account.class);
        Account account =(Account) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        criteria.add(Restrictions.eq("agent_id",account.getId()));
        if (StringUtils.isNotEmpty(keyWord)){
            if ("男".equals(keyWord)){
                criteria.add(Restrictions.or(Restrictions.like("name","%"+keyWord+"%", MatchMode.ANYWHERE),
                        Restrictions.like("phone","%"+keyWord+"%", MatchMode.ANYWHERE),
                        Restrictions.like("address","%"+keyWord+"%", MatchMode.ANYWHERE),
                        Restrictions.like("idCard","%"+keyWord+"%", MatchMode.ANYWHERE),
                        Restrictions.like("sex",0)));
            }else{
                criteria.add(Restrictions.or(Restrictions.like("name","%"+keyWord+"%", MatchMode.ANYWHERE),
                        Restrictions.like("phone","%"+keyWord+"%", MatchMode.ANYWHERE),
                        Restrictions.like("address","%"+keyWord+"%", MatchMode.ANYWHERE),
                        Restrictions.like("idCard","%"+keyWord+"%", MatchMode.ANYWHERE),
                        Restrictions.like("sex",1)));
            }
        }
        criteria.addOrder(Order.desc("createDate"));
        List<Account> list = accountService.findByConditions(criteria, page, limit);
        return Layui.data(0, "success", accountService.getTotalCount(), list);

    }

    @ResponseBody
    @RequestMapping(value = "deleteSaleMan")
    public Layui delete(@RequestParam(value = "id",required = false) Integer id){
        try {
            Account account = accountService.findById(id);
            accountService.delete(account);
            return Layui.data(0,"操作成功",0,null);
        }catch (Exception e){
            e.printStackTrace();
            return Layui.data(0,"删除失败",0,null);
        }

    }

    @ResponseBody
    @RequestMapping(value = "findUpSale")
    public MsgInfo findUpSale(@RequestParam(value = "sid",required = false)Integer sid){
        MsgInfo msgInfo = new MsgInfo();
        try {
            Account account = accountService.findById(sid);
            msgInfo.setData(account);
            return msgInfo;
        }catch (Exception e){
            e.printStackTrace();
            return msgInfo;
        }
    }

    @ResponseBody
    @RequestMapping(value = "findBelong")
    public MsgInfo findBelong(@RequestParam(value = "aid",required = false)Integer aid){
        MsgInfo msgInfo = new MsgInfo();
        Account account = accountService.findById(aid);
        msgInfo.setData(account);
        return msgInfo;
    }


}
